<template>
    <BPTable ref='tableRef' align='center' row-key='id' name-key='name' :columns='columns' :api='page' :buttons='buttons'>
        <template #name="{record}">
            <a-space :size="16">
                <a-image v-if="record.cover" :width="30" :src="imageUrl + record.cover"></a-image>
                <div>{{ record.name }}</div>
            </a-space>
        </template>
    </BPTable>
    <BDrawer v-model:visible='visible' :title='dialogTitle' width='800px' :confirm='submit'>
        <Edit :row="currentRow" :category="category" ref="formRef" />
    </BDrawer>
</template>

<script lang='ts' setup>

    import { pageApi, deleteApi } from '@/api/Bianli'
    import { message, Modal } from 'ant-design-vue'
    import { ref } from 'vue'
    import { useI18nStore } from '@/store/i18n'
    import { useResourceStore } from '@/store/resource'
    import Edit from './Edit.vue';
    import { parseTime, recursionFn } from '@/utils';

    const props = defineProps<{
        category : any
    }>()

    function page(params) {
        return pageApi({...params, categoryId: props.category.id})
    }


    const imageUrl = import.meta.env.B_NET_STATIC_URL

    const i18n = useI18nStore()
    const resourceStore = useResourceStore()

    const visible = ref(false)
    const formRef = ref()
    const dialogTitle = ref()

    const tableRef = ref()

    const currentRow = ref<any>()
    const currentCategory = ref()

    function onChangeCategory(e) {
        currentCategory.value = e
        tableRef.value.load()
    }


    const buttons = ref<SearchButton[]>([])

    buttons.value.push({
        button : 'add',
        func : () => {
            currentRow.value = null
            dialogTitle.value = '新增'
            visible.value = true
        }
    })


    const columns = ref<Column[]>([
        {
            title : '名称',
            dataIndex : 'name',
            inSearch : true,
        },

        // {
        //     title : "分类",
        //     dataIndex : 'category',
        //     translate : true,
        //     inSearch : true,
        //     type : 'treeSelect',
        //     options : async () => {
        //         const data = await categoryApi()
        //         data.forEach(item => {
        //             item.name = i18n.t(item.name)
        //         })
        //         return recursionFn(data, Math.min(...data.map(r => r.parentId)), 'id', 'parentId', 'children')
        //     },
        //     labelField : 'name',
        //     valueField : 'id',
        //     name : 'categoryId'
        // },

        {
            title : '排序',
            dataIndex : 'sort',
        },

        {
            title : '创建时间',
            dataIndex : 'createTime',
            customRender({ text }) {
                return parseTime(text)
            }
        },

        {
            title : '修改时间',
            dataIndex : 'updateTime',
            customRender({ text }) {
                return parseTime(text)
            }
        },

        {
            title : i18n.t('operation'),
            dataIndex : '$action',
            align : 'center',
            width : 150,
            $edit : (row) => {
                currentRow.value = row
                dialogTitle.value = '修改'
                visible.value = true
            },

            $delete : async (row) => {
                const res = await deleteApi(row.id)
                if(res.success) {
                    message.success(i18n.t('deleteSuccess'))
                    tableRef.value.load()
                }else {
                    message.error(res.msg)
                }
            }
        }
    ])


    async function submit() {
        const res = await formRef.value.submit()
        if(res.success) {
            tableRef.value.reload()
            visible.value = false
            message.success('保存成功')
        }else {
            Modal.error({
                title : res.msg
            })
        }
    }

    
</script>

<style lang="scss" module>
    .container{
        display: flex;
        .category{
            width: 250px;
            border-right: 1px solid $borderColor;
        }
        .table{
            flex: 1;
        }
    }
</style>